package com.concurrent.sample.business.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.concurrent.sample.business.dto.debug.AppDebugInfoDTO;
import com.concurrent.sample.business.service.impl.CashFlowServiceImpl;
import com.concurrent.sample.business.service.impl.PositionServiceImpl;
import com.concurrent.sample.business.dto.debug.DataMemoryTypeEnum;
import java.util.HashMap;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(path = "debug")
@Slf4j
@RequiredArgsConstructor
public class AppDebugInfoController {


  /**
   * http://localhost:8081/debug/initDataMemoryType?dataMemoryType=DB
   * @return
   */
  @GetMapping(path = "/initDataMemoryType")
  public Map<String, Object> initDataMemoryType(@RequestParam String dataMemoryType){
    Map<String, Object> map = new HashMap<>();
    AppDebugInfoDTO.dataMemoryType = DataMemoryTypeEnum.valueOf(dataMemoryType);
    map.put("dataMemoryType", dataMemoryType);
    return map;
  }

  /**
   * http://localhost:8081/debug/reportTime
   * @return
   */
  @GetMapping(path = "/reportTime")
  public String reportTime(){
    return (AppDebugInfoDTO.endTime - AppDebugInfoDTO.startTime) + "(ms)";
  }

  /**
   * http://localhost:8081/debug/init
   * @return
   */
  @GetMapping(path = "/init")
  public Map<String, Object> initCache(){
    Map<String, Object> map = new HashMap<>();
    SpringUtil.getBean(CashFlowServiceImpl.class).init();
    SpringUtil.getBean(PositionServiceImpl.class).init();
    AppDebugInfoDTO.startTime = null;
    AppDebugInfoDTO.endTime = null;
    map.put("cash", CashFlowServiceImpl.CUSTTOMERNO_CONTAINER);
    map.put("position", PositionServiceImpl.ID_CONTAINER);
    return map;
  }

  /**
   * http://localhost:8081/debug/localCache
   * @return
   */
  @GetMapping(path = "/localCache")
  public Map<String, Object> cashLocalCache(){
    Map<String, Object> map = new HashMap<>();
    map.put("cash", CashFlowServiceImpl.CUSTTOMERNO_CONTAINER);
    map.put("position", PositionServiceImpl.ID_CONTAINER);
    return map;
  }

}
